package cn.org.qary.controller.yaokaiing.back;

import cn.org.qary.entity.AddressInfo;
import cn.org.qary.entity.Order;
import cn.org.qary.service.trait.abelianymph.OrderRecordService;
import cn.org.qary.service.trait.abelianymph.OrderService;
import cn.org.qary.service.trait.rking.AddressInfoService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.vavr.Tuple2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller("back_OrderController")
@RequestMapping("back_order")
public class OrderController {
    @Autowired
    AddressInfoService as;
    @Autowired
    OrderService os;
    @Autowired
    OrderRecordService ors;

    int rows=3;
    @RequestMapping("show/{page}")
    public String findAll(@PathVariable(required = false) Integer page,Model model){
        if (page==null) page=1;
        Page<Object> pages = PageHelper.startPage(page, rows);
        List<Order> list=os.selectAll();


        List<Tuple2<Order, AddressInfo>> aList = new ArrayList<>();
        for (Order o : list) {
            AddressInfo addressInfo = as.selectById(o.getAddressInfoId());
            Tuple2<Order, AddressInfo> tempTuple2 = new Tuple2<>(o, addressInfo);
            aList.add(tempTuple2);
        }
        model.addAttribute("order",aList);
        model.addAttribute("pages",pages);
        return "manage/order";
    }

    @RequestMapping("showOne/{id}")
    public String showOne(@PathVariable String id,Model model){
        Order order=os.selectById(id);
        model.addAttribute("order",order);
        return "manage/order-modify";
    }

    @RequestMapping("update")
    public String updateorder(Order order){
        os.update(order);
        return "redirect:show/1";
    }


}
